package com.liuxingzhiwu.ruijishangcheng.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.liuxingzhiwu.ruijishangcheng.common.R;
import com.liuxingzhiwu.ruijishangcheng.dto.SetmealDto;
import com.liuxingzhiwu.ruijishangcheng.entity.Setmeal;
import com.liuxingzhiwu.ruijishangcheng.entity.SetmealDish;
import com.liuxingzhiwu.ruijishangcheng.service.SetmealDishService;
import com.liuxingzhiwu.ruijishangcheng.service.SetmealService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;

@RestController
@Slf4j
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private SetmealDishService setmealDishService;

    /**
     * 分页查询
     * @param page
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/page")
    public R<Page> page(int page,int pageSize,String name){

        //设置分页构造器
        Page<Setmeal> pageInfo = new Page<>(page,pageSize);

        LambdaQueryWrapper<Setmeal> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(StringUtils.isNotEmpty(name),Setmeal::getName,name);

        setmealService.page(pageInfo,queryWrapper);

        return R.success(pageInfo);
    }

    //GET http://localhost:8080/setmeal/list?categoryId=1413342269393674242&status=1
    @GetMapping("/list")
    public R<List> getSetmeal(Long categoryId,Integer status){

        QueryWrapper queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("category_id",categoryId);
        queryWrapper.eq("status",status);

        List<Setmeal> list = setmealService.list(queryWrapper);

        return R.success(list);
    }

    /**
     * 新增套餐，套餐由菜品组成
     * @param request
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R<String> save(HttpServletRequest request, @RequestBody SetmealDto setmealDto){

        setmealService.saveWithSetmealDish(request,setmealDto);
        return R.success("添加成功");
    }

    /**
     * 回显套餐信息
     * @param id 套餐id
     * @return
     */
    @GetMapping("{id}")
    public R<SetmealDto> getEditSetmeal(@PathVariable("id") Long id){

        //1、查询套餐
        Setmeal setmeal = setmealService.getById(id);

        //2、查询套餐中的菜品
        LambdaQueryWrapper<SetmealDish> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishService.list(queryWrapper);

        //3、将套餐名称和套餐菜品组合为数据传输对象
        SetmealDto setmealDto = new SetmealDto();
        setmealDto.setSetmealDishes(setmealDishes);
        BeanUtils.copyProperties(setmeal,setmealDto);

        return R.success(setmealDto);
    }

    /**
     * 修改套餐
     * @param request
     * @param setmealDto
     * @return
     */
    @PutMapping
    public R<String> updateSetmeal(HttpServletRequest request,@RequestBody SetmealDto setmealDto){
        setmealService.updateWithSetmealDish(request,setmealDto);
        return R.success("修改成功");
    }

    /**
     * (批量)停售和起售
     * @param request
     * @param status
     * @param ids
     * @return
     */
    @PostMapping("/status/{status}")
    public R<String> updateStatus(HttpServletRequest request,@PathVariable int status,Long[] ids){
        Long empId = (Long) request.getSession().getAttribute("employee");

        Setmeal setmeal = new Setmeal();
        for (Long id:ids) {
            setmeal.setId(id);
            setmeal.setStatus(status);
            setmeal.setUpdateTime(LocalDateTime.now());
            setmeal.setUpdateUser(empId);
            setmealService.updateById(setmeal);
        }

        return R.success("修改成功");
    }

    /**
     * 删除套餐和菜品
     * @param request
     * @param ids
     * @return
     */
    @DeleteMapping
    @Transactional
    public R<String> deleteSetmeal(HttpServletRequest request,Long[] ids){

        for (Long id:ids) {
            //删除套餐
            QueryWrapper<Setmeal> queryWrapper = new QueryWrapper<>();
            queryWrapper.eq("id",id);
            setmealService.remove(queryWrapper);

            //删除套餐菜品
            QueryWrapper<SetmealDish> queryWrapper1 = new QueryWrapper<>();
            queryWrapper1.eq("setmeal_id",id);
            setmealDishService.remove(queryWrapper1);
        }

        return R.success("删除成功");
    }

}
